package com.sinosoft.hydra.um.services.component.dataCollect.readers;

import com.sinosoft.hydra.logging.LogModules;
import com.sinosoft.hydra.um.domain.model.LabDataParser;
import com.sinosoft.hydra.um.services.component.dataCollect.DataParser;
import com.sinosoft.hydra.um.services.component.dataCollect.DataReader;
import com.sinosoft.hydra.um.services.component.dataCollect.DataResult;
import org.apache.commons.lang.StringUtils;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.sql.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Properties;

/**
 * Created by Administrator on 2016/8/8.
 */
public class MdbReader implements DataReader {


    public DataResult read(List<String> paths, Date begin, Date end, DataParser parser, LabDataParser labDataParser) {
        DataResult dataResult = new DataResult();
        for (String path : paths) {
            File file = new File(path);
            if (!file.exists()) {
                LoggerFactory.getLogger(LogModules.STD_LOGGER).error("文件路径{}未找到", path);
            }
            try {
                readMdb(path, begin, end, parser, dataResult, labDataParser);
            } catch (Exception e) {
                LoggerFactory.getLogger(LogModules.STD_LOGGER).error("csv文件解析失败，没有获取到数据{}", e.getMessage());
                return null;
            }
        }
        return dataResult;
    }

    private void readMdb(String path, Date begin, Date end, DataParser parser, DataResult dataResult, LabDataParser labDataParser) throws Exception {
        Properties prop = new Properties();
        prop.put("charSet", "gb2312");                //这里是解决中文乱码
        prop.put("user", "");
        prop.put("password", "");
        String url = "jdbc:odbc:driver={Microsoft Access Driver (*.mdb)};DBQ=" + path;   //文件地址
        PreparedStatement ps = null;
        Statement stmt = null;
        ResultSet rs = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection conn = DriverManager.getConnection(url, prop);
            stmt = conn.createStatement();
            rs = stmt.executeQuery("select * from historytable");
            ResultSetMetaData data = rs.getMetaData();
            rs.next();
            List<String> titles = new ArrayList<String>();
            for (int i = 1; i < data.getColumnCount() + 1; i++) {
                titles.add(data.getColumnName(i));
            }
            int last = labDataParser.getCellsNum() == -1 ? titles.size() : labDataParser.getCellsNum();
            dataResult.addTitles(titles, last, parser);
            while (rs.next()) {
                List<String> cells = new ArrayList<String>();
                for (int i = 1; i < last + 1; i++) {
                    String columnValue = rs.getString(i);
                    cells.add(StringUtils.isEmpty(columnValue) ? "" : columnValue);
                }
                if (!parser.valid(cells)) {
                    break;
                }
                dataResult.addData(parser.parse(cells, begin, end), last, parser);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            throw new RuntimeException("读取数据出错");
        }

    }
}
